package com.qfedu.dao;

import com.qfedu.model.Contractor;
import com.qfedu.model.Developer;
import com.qfedu.model.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

public interface ProjectDao {




    //添加项目
    @Insert("insert into project(pname,starttime,endtime,did,cid,updatetime,status)" +
            " values(#{pname},#{starttime},#{endtime},#{did},#{cid},#{updatetime},#{status})")
    void addProject(Project project);

    //物理删除项目
    @Update("update project set status = 0 where pid=#{pid}")
    void deleteProject(int pid);

    //修改项目
    @Update("update project set  pname=#{pname},starttime=#{starttime},endtime=#{endtime}," +
            "did=#{did},cid=#{cid},updatetime=#{updatetime},status=#{status} where pid=#{pid}")
    void updateProject(Project project);

    //根据pid查询
    @Select("select * from project where status = 1 and pid = #{pid}")
    Project findByPid(int pid);

    //根据cid查询
    @Select("select * from project where status = 1 and cid =#{cid}")
    List<Project> findByCid(String cid);

    //根据cid查询
    @Select("select * from project where status = 1 and did =#{did}")
    List<Project> findByDid(String did);


    //查询项目
    @Results(id = "selectAllMap",value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "pname",property = "pname"),
            @Result(column = "starttime",property = "starttime"),
            @Result(column = "endtime",property = "endtime"),
            @Result(column = "did",property = "did"),
            @Result(column = "cid",property = "cid"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "status",property = "status"),
            @Result(column = "cid",property = "contractor",javaType = Contractor.class,
                    one = @One(select = "com.qf.dao.ContractorDao.findByCid" ,fetchType = FetchType.EAGER)),
            @Result(column = "did",property = "developer",javaType = Developer.class,
                    one = @One(select = "com.qf.dao.DeveloperDao.findByDid" ,fetchType = FetchType.EAGER)),
            @Result(column = "pid",property = "stages",
                    one = @One(select = "com.qf.dao.StageDao.findByPid" ,fetchType = FetchType.EAGER)),
            @Result(column = "pid",property = "branches",
                    one = @One(select = "com.qf.dao.BranchDao.findByPid" ,fetchType = FetchType.EAGER)),
            @Result(column = "pid",property = "subitems",
                    one = @One(select = "com.qf.dao.SubitemDao.findByPid" ,fetchType = FetchType.EAGER)),
    })
    @Select("select * from project where status=1")
    List<Project> findAll();
}
